package fr.enseirb.mycub.activity;

import fr.enseirb.mycub.R;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;

public class SettingsPreferencesActivity extends AbstractPreferencesActivity {

	protected CheckBoxPreference mDistanceFilterCb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.location_preferences);
		//addPreferencesFromResource(R.xml.map_preferences);
		addPreferencesFromResource(R.xml.filter_preferences);
		addPreferencesFromResource(R.xml.other_preferences);
		PreferenceScreen preferenceScreen = getPreferenceScreen();
		SharedPreferences preferences = preferenceScreen.getSharedPreferences();
		mNetworkPreference = preferenceScreen.findPreference(AbstractPreferencesActivity.NETWORK_PREFERENCE);
		mReportBugPreference = preferenceScreen.findPreference(AbstractPreferencesActivity.REPORT_BUG_PREFERENCE);
		mFeedbackPreference = preferenceScreen.findPreference(AbstractPreferencesActivity.FEEDBACK_PREFERENCE);
		mNetworkPreference.setSummary(preferences.getString(NETWORK_NAME, "Bordeaux")+ " (" + preferences.getString(NETWORK_CITY, "Bordeaux")+")");
		preferences.registerOnSharedPreferenceChangeListener(this);
		mDistanceFilterCb = (CheckBoxPreference) getPreferenceScreen()
				.findPreference(
						AbstractPreferencesActivity.ENABLE_DISTANCE_FILTER);
		getPreferenceScreen().getSharedPreferences()
		.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
		mDistanceFilterCb.setEnabled(prefs.getBoolean(
				AbstractPreferencesActivity.LOCATION_PREFERENCE, true));
		mDistanceFilterCb
		.setSummary(mDistanceFilterCb.isEnabled() ? R.string.enable_distance_filter_summary
				: R.string.enable_location_before);
		getPreferenceScreen().findPreference(
				AbstractPreferencesActivity.DISTANCE_FILTER).setSummary(
						getString(R.string.distance_filter_summary)
						+ " "
						+ prefs.getInt(
								AbstractPreferencesActivity.DISTANCE_FILTER,
								1000) + "m");
		super.onResume();
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {
		/*if (preference == mNetworkPreference) {
			SharedPreferences prefs = preferenceScreen.getSharedPreferences();
			BikeDBAdapter.getInstance(this).setUpdateVersion(prefs.getLong(STATIONS_VERSION, 0),prefs.getInt(NETWORK_PREFERENCE, 0));
			startActivity(new Intent(this, MenuActivity.class).setAction(
					MenuActivity.ACTION_CHOOSE_NETWORK).setFlags(
					Intent.FLAG_ACTIVITY_CLEAR_TOP));
		} else */ 
		if (preference == mReportBugPreference) {
			final Intent emailIntent = new Intent(
					android.content.Intent.ACTION_SEND);
			emailIntent.setType("plain/text");
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
					new String[] { "coudar@enseirb-matmeca.fr" });
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
					"Bug Inertness");
			startActivity(Intent.createChooser(emailIntent, "Signaler un bug"));
		}

		if (preference == mFeedbackPreference) {
			//AppGardenAgent.askForFeedback(this);
		}
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		super.onSharedPreferenceChanged(sharedPreferences, key);
		if (key.equals(AbstractPreferencesActivity.DISTANCE_FILTER)) {
			getPreferenceScreen()
			.findPreference(AbstractPreferencesActivity.DISTANCE_FILTER)
			.setSummary(
					getString(R.string.distance_filter_summary)
					+ " "
					+ sharedPreferences
					.getInt(
							AbstractPreferencesActivity.DISTANCE_FILTER,
							1000) + "m");
		}
	}

}
